/**  
 * All rights Reserved, Designed By <a href="www.itttq.com" target="_blank">www.itttq.com</a>
 * @date:   2017-07-21 11:42:21
 * @Copyright: 2017 <a href="www.itttq.com" target="_blank">www.itttq.com</a> Inc. All rights reserved. 
 */
package com.itttq.gshop.pbpm.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.itttq.gshop.domain.po.SysUserPo;
import com.itttq.gshop.domain.to.ResultPageTo;
import com.itttq.gshop.generic.controller.GenericController;
import com.itttq.gshop.pbpm.service.SysUserService;

@RestController
@RequestMapping("sysUser")
public class SysUserController extends GenericController {
	private static final long serialVersionUID = -4456967196179870L;
	public static final String CACHE_KEY = "sysUser";
	@Autowired
	private SysUserService sysUserService;
	
	/**
	 * 根据条件获取SysUserPo对象，并返回查询的数据总数
	 * 
	 * @param sysUser
	 * @Author 迷蝶
	 * @return ResultPageTo<SysUserPo>
	 */
	@RequestMapping("list")
    public ResultPageTo<SysUserPo> getSysUsers(@RequestBody SysUserPo sysUser) {
        return sysUserService.getSysUsers(sysUser);
    }
	/**
	 * 用户登录
	 * 
	 * @param username	用户名
	 * @param password	密码
	 * @return
	 * @Author 迷蝶
	 * @return SysUserPo	为null表示用户名或密码错误
	 */
	@RequestMapping("login")
	public SysUserPo login(String username, String password) {
		return sysUserService.login(username, password);
	}
	
	/**
	 * 根据ID获取SysUserPo对象，并放入redis缓存
	 * 
	 * @param id	只能是数字
	 * @Author 迷蝶
	 * @return SysUserPo
	 */
	@RequestMapping("/{id:^[0-9]*$}")
	@Cacheable(value=CACHE_KEY, key="'sysUser_'+#id")
	public SysUserPo getSysUser(@PathVariable("id") int id) {
		return sysUserService.getSysUser(id);
	}
	
	/**
	 * 保存SysUserPo对象，并清除此ID的缓存。<br>
	 * 如果ID存在，则更新对象；ID不存在则插入对象。
	 * 
	 * @param sysUser
	 * @Author 迷蝶
	 * @return SysUserPo
	 */
	@RequestMapping("/save")
	@CacheEvict(value=CACHE_KEY, key="'sysUser_'+#sysUser.id")
	public Integer saveSysUser(@RequestBody SysUserPo sysUser) {
		int rs = 0;
		if(null == sysUser) {return rs;}
		if(null == sysUser.getId()) {
			rs = sysUserService.addSysUser(sysUser);
		} else {
			rs = sysUserService.updateSysUser(sysUser);
		}
		return rs;
	}
	
	/**
	 * 根据ID删除SysUserPo对象，并清除此ID的缓存。<br>
	 * 
	 * @param id	只能是数字
	 * @Author 迷蝶
	 * @return SysUserPo
	 */
	@RequestMapping("/del/{id:^[0-9]*$}")
	@CacheEvict(value=CACHE_KEY, key="'sysUser_'+#id")
	public Integer delSysUser(@PathVariable("id") int id) {
		return sysUserService.delSysUser(id);
	}
}